/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package opt.Impl;


import dao.AccountDao;
import dao.Impl.AccountDaoImpl;
import dao.Impl.PetDaoImpl;
import dao.Impl.PetOwnerDaoImpl;
import dao.Impl.PetStoreDaoImpl;
import dao.PetOwnerDao;
import dao.PetStoreDao;
import entity.Account;
import entity.Pet;
import entity.PetOwner;
import entity.PetStore;
import java.sql.Date;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import static opt.Client.getPet;
import opt.PetOwnerService;

/**
 *
 * @author 10592
 */
public class PetOwnerServiceImpl implements PetOwnerService{
    Scanner scanner = new Scanner(System.in);
    PetOwner petOwner;
    String  password;int account;


    public PetOwnerServiceImpl() {
       
    }

    @Override
    public void buy(Pet pet) {
        if (petOwner.getMoney()>pet.getPrice()) {
            
            int masterid=pet.getMasterId();
            //更新宠物信息
            PetDaoImpl petDao = new PetDaoImpl();
            petDao.updata(pet.getId(), "宠物主人", petOwner.getId());
            //更新宠物主人信息
            PetOwnerDao ownerDao = new PetOwnerDaoImpl();
            ownerDao.updata(petOwner.getId(),petOwner.getMoney()-pet.getPrice());
            //更新宠物商店信息
            PetStoreDao instance = new PetStoreDaoImpl();
            PetStore petStore = instance.getBtId(masterid);
            instance.updata(petStore.getId(),petStore.getBalance()+pet.getPrice());
           
        }
        else
        {
            System.out.println("您的余额不足，请重新选择");
        }
        
    }

    @Override
    public void sell(Pet pet) {
        System.out.println("---------下面是现有宠物商店，请选择您要卖给的买家序号------------");
        PetStoreDao instance = new PetStoreDaoImpl();
        List<PetStore> petStores = instance.getlist();
        System.out.println("商店编号" + "\t" + "商店名称" + "\t" + "\t" + "商店金额");
        for (int i = 0; i < petStores.size(); i++) {
            System.out.print(petStores.get(i).getId() + "\t");
            System.out.print(petStores.get(i).getName() + "\t");
            System.out.print(petStores.get(i).getBalance() + "\t");
            System.out.println();
        }
        while (!scanner.hasNextInt()) {
            System.out.println("输入错误，请重新输入");
            scanner.next();
        }
        int num = scanner.nextInt();
        if (num > petStores.size()) {
            System.out.println("没有该商店。");
        } else {
            //更新宠物商店信息
            PetStore petStore=instance.getBtId(num);
            instance.updata(petStore.getId(), petStore.getBalance()-pet.getPrice());
            //更新宠物信息
            PetDaoImpl petDao = new PetDaoImpl();
            petDao.updata(pet.getId(), "店主", petStore.getId());
            //更新宠物主人信息
            PetOwnerDao ownerDao = new PetOwnerDaoImpl();
            ownerDao.updata(petOwner.getId(), petOwner.getMoney()+pet.getPrice());
            //更新账单   
            try {
                Date date = new Date(System.currentTimeMillis());
                Account account = new Account(2, pet.getPrice(),pet.getId(), petOwner.getId(), petStore.getId(), date);
                AccountDao accountDao = new AccountDaoImpl();
                accountDao.add(account);
            } catch (SQLException ex) {
                Logger.getLogger(PetOwnerServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    @Override
    public PetOwner login() {
        System.out.println("宠物主人你好，请您输入主人账号：");
        account = scanner.nextInt();
        System.out.println("宠物主人你好，请您输入主人密码：");
        password = scanner.next();
        PetOwnerDao instance = new PetOwnerDaoImpl();
        petOwner = instance.login(account, password);
        if (petOwner != null) {
            System.out.println("主人编号" + "\t" + "主人名称" + "\t" + "主人密码" + "\t" + "主人金额");
            System.out.println("-----------恭喜您登录成功--------------");
            System.out.println("-----------您的基本信息：--------------");
            System.out.println("姓名：" + petOwner.getName());
            System.out.println("金额：" + petOwner.getMoney());
        } else {
            System.out.println("用户名或密码错误，请重输入");
        }
        return petOwner;
    }
}
